package com.ruoyi.service.impl;

import com.ruoyi.domain.MobilEvaluate;
import com.ruoyi.domain.MobilMerchant;
import com.ruoyi.domain.MobilServiceDetails;
import com.ruoyi.mapper.MobilEvaluateMapper;
import com.ruoyi.mapper.MobilMerchantMapper;
import com.ruoyi.mapper.MobilServiceDetailsMapper;
import com.ruoyi.service.IMobilMerchantService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 手机端商户Service业务层处理
 *
 * @author ruoyi
 * @date 2025-01-20
 */
@Service
public class MobilMerchantServiceImpl implements IMobilMerchantService {
    @Autowired
    private MobilMerchantMapper mobilMerchantMapper;

    @Autowired
    private MobilServiceDetailsMapper mobilServiceDetailsMapper;

    @Autowired
    private MobilEvaluateMapper mobilEvaluateMapper;

    /**
     * 查询商户列表
     *
     * @param mobilMerchant 商户
     * @return 商户集合
     */
    @Override
    public List<MobilMerchant> selectMobilMerchantList(MobilMerchant mobilMerchant) {
        return mobilMerchantMapper.selectMobilMerchantList(mobilMerchant);
    }

    /**
     * 查询附近商户列表
     *
     * @param longitude 经度
     * @param latitude 纬度
     * @param radius 半径（公里）
     * @param serviceType 服务类型
     * @return 商户集合
     */
    @Override
    public List<MobilMerchant> selectNearbyMerchants(String longitude, String latitude, Double radius, Integer serviceType) {
        List<MobilMerchant> merchants = mobilMerchantMapper.selectNearbyMerchants(longitude, latitude, radius, serviceType);
        
        // 为每个商户设置图片和部分服务信息
        for (MobilMerchant merchant : merchants) {
            // 设置商户图片
            List<String> images = mobilMerchantMapper.selectMerchantImages(merchant.getId());
            merchant.setImages(images);
            
            // 设置部分服务项目（最多2个用于展示）
            List<MobilServiceDetails> services = mobilServiceDetailsMapper.selectServicesByMerchantId(merchant.getId());
            if (services.size() > 2) {
                services = services.subList(0, 2);
            }
            merchant.setServices(services);
        }
        
        return merchants;
    }

    /**
     * 根据主键查询商户详情
     *
     * @param id 商户主键
     * @return 商户
     */
    @Override
    public MobilMerchant selectMobilMerchantById(Integer id) {
        return mobilMerchantMapper.selectMobilMerchantById(id);
    }

    /**
     * 获取商户完整信息（包括图片、服务、评价等）
     *
     * @param id 商户主键
     * @return 商户完整信息
     */
    @Override
    public MobilMerchant getMerchantCompleteInfo(Integer id) {
        // 获取商户基本信息
        MobilMerchant merchant = mobilMerchantMapper.selectMobilMerchantById(id);
        if (merchant == null) {
            return null;
        }

        // 设置商户图片
        List<String> images = mobilMerchantMapper.selectMerchantImages(id);
        merchant.setImages(images);

        // 设置服务项目
        List<MobilServiceDetails> services = mobilServiceDetailsMapper.selectServicesByMerchantId(id);
        merchant.setServices(services);

        return merchant;
    }
}











